package com.sqly.doc.menu.tnb;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.adapter.CommonAdapter;
import com.sqly.doc.adapter.ViewHolder;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.pojo.TnbHzsf;
import com.sqly.doc.util.DateConvert;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.NetWorkUtils;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.StringUtils;
import com.sqly.doc.view.XListView;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * 高血压患者随访记录列表
 * Created by Administrator on 2017/11/28 0028.
 */

public class tnb_list extends MyBaseActivity{
    XListView listView;
    //标题
    TextView title, uname;
    LinearLayout back;
    ZLoadingDialog dialog;//加载动画
    Context con = tnb_list.this;
    private int pageNum1 = 1;// 无关键字页数
    List<TnbHzsf> list;
    private ImageView cx;
    private EditText key;
    @Override
    protected void initUI() {
        setContentView(R.layout.tnb_fsjl_list);
        cx = (ImageView) findViewById(R.id.cx);
        cx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pageNum1 = 1;
                getJumin();
            }
        });
        key = (EditText) findViewById(R.id.key);
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        title.setText("II型糖尿病患者随访服务记录");
        uname.setText("新增");
        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(con, tnb.class).putExtra("type", "add"));
            }
        });
        back = (LinearLayout) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        listView = (XListView) findViewById(R.id.listView);
        listView.setPullLoadEnable(true);
        dialog = new ZLoadingDialog(con);
        dialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)//设置类型
                .setLoadingColor(Color.parseColor("#1C86EE"))//颜色
                .setHintText("Loading...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(Color.parseColor("#1C86EE"));  // 设置字体颜色
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle b = new Bundle();
                b.putSerializable("b", list.get(i- 1));
                startActivity(new Intent(con, tnb.class).putExtras(b).putExtra("type", "edit"));
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(NetWorkUtils.isNetworkConnected(con)){//联网状态
            getJumin();
        }
    }

    @Override
    protected void initListener() {

    }

    public void getJumin() {
        RequestParams ps = new RequestParams();
        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
        ps.add("type", "2");
        ps.add("page", pageNum1 + "");
        if (!StringUtils.isEmpty(key.getText().toString()))
            ps.add("XINGMING", key.getText().toString());
        UserClient.get(HttpUrl.GetTnbfsjlList, ps, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                MyToastUtil.ShowToast(con, "请求失败");
            }
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                String content = new String(response);
                dialog.dismiss();
                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                if (ret.getCode().equals("200")) {
                    List<TnbHzsf> ll = JSON.parseArray(ret.getList(), TnbHzsf.class);
                    if (pageNum1 == 1)
                        list = new ArrayList<TnbHzsf>();
                    for (int i = 0; i < ll.size(); i++) {
                        list.add(ll.get(i));
                    }
                    listView.setSelection(list.size() - ll.size());
                    listView.stopRefresh();
                    listView.stopLoadMore();
                    listView.setRefreshTime("刚刚");
                    listView.setXListViewListener(new XListView.IXListViewListener() {
                        @Override
                        public void onRefresh() {
                            pageNum1 = 1;
                            getJumin();
                        }

                        @Override
                        public void onLoadMore() {
                            pageNum1 = pageNum1 + 1;
                            getJumin();
                        }
                    });
                    listView.setAdapter(new CommonAdapter<TnbHzsf>(con, list, R.layout.ycf_chfsjl_item) {
                        @Override
                        public void convert(ViewHolder helper, TnbHzsf item) {
                            helper.setText(R.id.xm, item.getXINGMING());
                            helper.setText(R.id.jkdah, "健康档案号:"+item.getJKDAH());
                            helper.setText(R.id.sfrq, DateConvert.dateToString(DateConvert.toDate(item.getSFRQ(), DateConvert.YMD), DateConvert.YMD)+"    ");
                        }
                    });
                    /////////////////////////////////////////////////////////////////////////本地化
                    /*List<JsbSf> dblist = DataSupport.where("CARDNUM=?",SharedPreferencesUtils.getParam(con,"cardnum","").toString()).find(JsbSf.class);
                    if (dblist.size() > 0) {//
                        for (int i = 0; i < ll.size(); i++) {
                            List<JsbSf> jlist = DataSupport.where("CARDNUM=?",SharedPreferencesUtils.getParam(con,"cardnum","").toString()).find(JsbSf.class);
                            if (jlist.size() > 0) {
                                if (jlist.get(0).getIsupdate().equals("1")) {//本地数据没有更新，可以删除添加新的
                                    Log.i("精神病数据", "删除后插入的数据");
                                    jlist.get(0).delete();
                                    ll.get(i).setIsupdate("1");//设置不需要更新的操作
                                    ll.get(i).save();
                                } else {//本地有数据保存还没有上传
                                    //不需要任何操作
                                }
                            } else {//本地数据没有
                                Log.i("精神病数据", "直接插入的数据");
                                ll.get(i).setIsupdate("1");//设置不需要更新的操作
                                ll.get(i).save();
                            }

                        }
                    } else {
                        Log.i("精神病数据", "直接插入的数据");
                        for (JsbSf j : ll) {
                            j.setIsupdate("1");//设置不需要更新的操作
                            j.save();
                        }
                    }*/
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                } else {
                    MyToastUtil.ShowToast(con, "获取错误");
                }
            }
        });
    }
}
